package 排序;
/**
 * @description：冒泡排序
 * @author: yangyahui01
 * @date: 12/27/24 2:05 PM
 */
public class 冒泡排序 {

    public static void main(String[] args) {
        int[] nums = {2, 7, 4, 6, 9, 1};
        bubblingSort(nums);
        print(nums);
    }


    public static void bubblingSort(int[] nums) {

        for (int i = 0; i < nums.length; i++) {

            boolean exchange = false; //判断是否还有可交换的元素,如果没有提前退出

            System.out.println("开始第" + (i + 1) + "轮");

            for (int j = 0; j < nums.length - i - 1; j++) {

                if (nums[j] > nums[j + 1]) {
                    int tmp = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = tmp;
                    exchange = true;
                }

                print(nums);
            }

            if (!exchange) {
                break;
            }
        }
    }

    private static void print(int[] nums) {
        for (int num : nums) {
            System.out.print(num + " ");
        }

        System.out.println();
    }
}
